// pages/login/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeArray: ['+1(加拿大/美国)'],
    codeIndex: 0,

    form: {
      code: '+1',
      mobile: '',
      yzm: '',
        is_agree: false,
        sms_code:''
    },

    // 倒计时
    showSecond: true,
    timeOut: 60, //获取验证码倒计时
    rid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if (options.rid) {
          this.setData({
            rid:options.rid
        })
    }
  },

  // 区号选择
  bindPickerChange: function(e) {
    let index = e.detail.value
    this.setData({
      codeIndex: index,
      'form.code': index==0?"+1":"+86"
    })
  },

  handleMobile: function(e) {
    this.setData({
        'form.mobile': e.detail.value
    })
  },
  handleYzm: function(e) {
    this.setData({
        'form.yzm': e.detail.value
    })
  },

  // 获取验证码
  getYzm() {
    let that = this
    let mobile = this.data.form.mobile;
    if (mobile == '') {
         app.m_showhint("请输入正确手机号");
         return false;
     }
    that.setData({
        showSecond: false
    }, () => {
        that.countDown()
        that.getcode(mobile);
    })
  },

  // 倒计时
  countDown() {
    let that = this
    let timeOut = that.data.timeOut
    if (timeOut > 1) {
        timeOut--
        that.setData({
          timeOut: timeOut
        })
        setTimeout(function () {
            that.countDown();
        }, 1000);
    } else {
        that.setData({
            showSecond: true,
            timeOut: 60
        })
    }
  },

    getcode: function (mobile) {
        let mh = this.data.codeIndex == 0 ? '1' : "86";
   // let code = this.data.form.code
    mobile = mh + mobile
     app.get('c=sys&a=mobilesend', { mobile: mobile }).then(data => {
         this.setData({
          ['form.sms_code']: data.codeid
        })
     })
    },
  
 
  mobilecheck: function () {
    let mobile = this.data.form.mobile
    let codeid = this.data.form.sms_code;
      let codenum = this.data.form.yzm;
      if (!this.data.form.is_agree) {
        app.m_showhint("请先同意协议");
        return false;
      }
      //mobile = code+mobile
      //console.log(mobile)
      //return;
    if (mobile == '' || codeid == "" || codenum == "") {
        app.m_showhint("请输入验证码");
        return false;
    }
      let code = this.data.form.code
      mobile = code+mobile
    /*this.setData({
        ['formData.yzm']: '',
        ['formData.sms_code']: '',
    })*/
      app.post("c=user&a=checkmobile", { mobile, codeid, codenum }).then(data => {
          if (this.data.rid == '') {
            let url = "/pages/user/user"
            app.m_tourl(url, "tab");
          } else {
            let url = "/pages/report/report-detail?rid="+this.data.rid
            app.m_tourl(url, "redirectto"); 
        }
        
    }).catch(err => {
        console.log(err)
        wx.hideLoading();
        wx.showToast({
            title: err.msg,
            icon: 'none'
        })

    })
},
  toggleAgree: function() {
    this.setData({
      'form.is_agree': !this.data.form.is_agree
    })
  },

  /*
   * 通用跳转
   */
  m_appurl: function (e) {
    let url = e.currentTarget.dataset.url;
    let type = '';
    if (e.currentTarget.dataset.type) {
      type = e.currentTarget.dataset.type;
    }
    app.m_tourl(url, type);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

 
})